package com.walker.springboot.rabbitmq.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @ProjectName: walker-step
 * @Package: com.walker.springboot.rabbitmq.listener
 * @ClassName: CommonListener
 * @Author: yangyl
 * @CreateDate: 2020/6/21 22:45
 */

@Component
public class CommonListener {

    protected Logger logger=LoggerFactory.getLogger(CommonListener.class);


    @Autowired
    protected ObjectMapper objectMapper;


//    @RabbitListener(queues = "${rabbitmq.config.basic.queue-key}",containerFactory = "multiListenerContainer")
//    public void receive(Message message, Channel channel){
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            byte[] body = message.getBody();
//            String x1=new String(body);
//            System.out.println("接受到消息"+x1);
//            int x=3/0;
//            channel.basicAck(deliveryTag, false);
//        }catch (Exception e){
//            e.printStackTrace();
//            logger.info("出现异常:{}}",e.fillInStackTrace());
//            try {
//                channel.basicReject(deliveryTag,true);
//            } catch (IOException e1) {
//                e1.printStackTrace();
//            }
//        }
//    }


    @RabbitListener(queues = "QUEUE1",containerFactory = "multiListenerContainer")
    public void receive2(Message message, Channel channel){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String receivedExchange = message.getMessageProperties().getReceivedExchange();
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        String consumerQueue = message.getMessageProperties().getConsumerQueue();

        try {
            Map<String, Object> headers = message.getMessageProperties().getHeaders();
            System.out.println(headers);
            byte[] body = message.getBody();
            String x1=new String(body);
            System.out.println("接受到消息2:"+x1);
            int x=3/0;
            channel.basicAck(deliveryTag, false);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("出现异常:{}}",e.fillInStackTrace());
            try {
                channel.basicReject(deliveryTag,false);
                //todo 三次失败，则拒绝
                Map<String,Object> headers=message.getMessageProperties().getHeaders();
                Integer retryCount= headers!=null && headers.get("retryCount")==null?0: (Integer) headers.get("retryCount");
                if(headers==null){
                    headers=new HashMap<String, Object>();
                }
                if(retryCount<=3){
                    headers.put("retryCount",++retryCount);
                    AMQP.BasicProperties basicProperties =  new AMQP.BasicProperties("text/plain",
                            null,
                            headers,
                            2,
                            0, null, null, null,
                            null, null, null, null,
                            null, null);
                    channel.basicPublish(receivedExchange,receivedRoutingKey,basicProperties,message.getBody());
                }

            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    @RabbitListener(queues = "warning.queue.01",containerFactory = "multiListenerContainer")
    public void receive3(Message message, Channel channel){
        System.out.println("message:"+message);
    }
}
